package com.yuerenjf.yuerenjinfu.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.http.Constants;
import com.yuerenjf.yuerenjinfu.http.Xutils;
import com.yuerenjf.yuerenjinfu.utils.CommonUtils;
import com.yuerenjf.yuerenjinfu.utils.ImageUtils;
import com.yuerenjf.yuerenjinfu.utils.MessageTimer;
import com.umeng.analytics.MobclickAgent;
import com.yuerenjf.yuerenjinfu.widget.SFProgrssDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class RetrieveActivity extends BaseActivity implements View.OnClickListener {
    private static final int SEND_CODE_AGAIN = 1;

    private EditText et_retrieve_username;
    private EditText et_retrieve_code;
    private TextView tv_retrieve_code;
    private TextView tv_retrieve_complete;
    private EditText et_retrieve_pwd;
    private EditText et_retrieve_again;


    private ImageView codeImage;
    EditText codeImageEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_title.setText("找回密码");

        ll_back.setOnClickListener(this);
        getCodeImage();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected View getChildView() {
        View view = View.inflate(this, R.layout.activity_retrieve, null);
        et_retrieve_username = (EditText) view.findViewById(R.id.et_retrieve_username);
        et_retrieve_code = (EditText) view.findViewById(R.id.et_retrieve_code);
        tv_retrieve_code = (TextView) view.findViewById(R.id.tv_retrieve_code);
        tv_retrieve_complete = (TextView) view.findViewById(R.id.tv_retrieve_complete);
        et_retrieve_pwd = (EditText) view.findViewById(R.id.et_retrieve_pwd);
        et_retrieve_again = (EditText) view.findViewById(R.id.et_retrieve_again);
        codeImage = (ImageView) view.findViewById(R.id.tv_register_code_image);
        codeImageEdit = (EditText) view.findViewById(R.id.et_register_code_number_edit);
        codeImage.setOnClickListener(this);
        tv_retrieve_code.setOnClickListener(this);
        tv_retrieve_complete.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_retrieve_code:
                if (!CommonUtils.isMobileNO(et_retrieve_username.getText().toString())) {
                    Toast.makeText(RetrieveActivity.this, "手机号码格式不正确", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(codeImageEdit.getText().toString())) {
                    Toast.makeText(RetrieveActivity.this, "请输入右侧图片的数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendCode(et_retrieve_username.getText().toString());
                break;
            case R.id.tv_retrieve_complete:
                if (!CommonUtils.isMobileNO(et_retrieve_username.getText().toString())) {
                    Toast.makeText(RetrieveActivity.this, "手机号码格式不正确", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(codeImageEdit.getText().toString())) {
                    Toast.makeText(RetrieveActivity.this, "请输入右侧图片的数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(et_retrieve_code.getText().toString())) {
                    Toast.makeText(RetrieveActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!CommonUtils.isPassword(et_retrieve_pwd.getText().toString())) {
                    Toast.makeText(RetrieveActivity.this, "密码需8-20位，至少包含字母和数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!et_retrieve_pwd.getText().toString().equals(et_retrieve_again.getText().toString())) {
                    Toast.makeText(RetrieveActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                    return;
                }


                retrieve();
                break;
            case R.id.ll_back:
                finish();
                break;

            case R.id.tv_register_code_image:
                getCodeImage();
                break;
        }
    }

    private void retrieve() {
        Map<String, String> map = new HashMap<>();
        map.put("phone", et_retrieve_username.getText().toString());
        map.put("password", et_retrieve_pwd.getText().toString());
        map.put("captcha", et_retrieve_code.getText().toString());
        map.put("pwd", et_retrieve_again.getText().toString());
        map.put("imagecode", codeImageEdit.getText().toString());
        final SFProgrssDialog dialog = SFProgrssDialog.show(RetrieveActivity.this, "");

        Xutils.getInstance().post(RetrieveActivity.this, Constants.URL_RETRIEVE, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                Toast.makeText(RetrieveActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                finish();

            }

            @Override
            public void onFailer(int status, String result) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (status == 300) {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                        JSONObject object = jsonObject.getJSONObject("data");
                        String base64Code = object.getString("img");
                        Bitmap bp = ImageUtils.stringtoBitmap(base64Code);
                        codeImage.setImageBitmap(bp);
                        codeImageEdit.setText("");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFinish() {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });

    }

    private void sendCode(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("imagecode", codeImageEdit.getText().toString());
        Xutils.getInstance().post(RetrieveActivity.this, Constants.URL_RETRIEVE_GET_CODE, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                Log.e("sendCode", result + "------");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    int status = jsonObject.getInt("status");

                    Toast.makeText(RetrieveActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
                    new MessageTimer(tv_retrieve_code, 90000, 1000).start();


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailer(int status, String result) {
                if (status == 300) {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                        JSONObject object = jsonObject.getJSONObject("data");
                        String base64Code = object.getString("img");
                        Bitmap bp = ImageUtils.stringtoBitmap(base64Code);
                        codeImage.setImageBitmap(bp);
                        codeImageEdit.setText("");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFinish() {

            }
        });

    }


    private void getCodeImage() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "3");
        Xutils.getInstance().get(RetrieveActivity.this, Constants.URL_REGISTER_AUTH_CODE, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONObject object = jsonObject.getJSONObject("data");
                    String base64Code = object.getString("img");
                    Bitmap bp = ImageUtils.stringtoBitmap(base64Code);
                    codeImage.setImageBitmap(bp);
                    codeImageEdit.setText("");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailer(int status, String result) {
                if (status == 300) {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                        JSONObject object = jsonObject.getJSONObject("data");
                        String base64Code = object.getString("img");
                        Bitmap bp = ImageUtils.stringtoBitmap(base64Code);
                        codeImage.setImageBitmap(bp);
                        codeImageEdit.setText("");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFinish() {

            }
        });


    }
}
